<script>
// let QQMapWX = require('@/common/js/qqmap-wx-jssdk.min.js')
import { mapState, mapGetters, mapMutations } from 'vuex';
export default {
  // 小程序全局变量，如果要是用状态管理，请使用vuex
  globalData: {
    userInfo: uni.getStorageSync('userInfo') || null,
    accessToken: uni.getStorageSync('accessToken') || null
  },
  onLaunch: function() {
    this.getLocation();
    // 为了首页加载页面
    // uni.hideTabBar()
    // 更新提示
    let that = this;
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate(function(res) {});
    updateManager.onUpdateReady(function() {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate();
          }
        }
      });
    });
    updateManager.onUpdateFailed(function() {
      // 新的版本下载失败
      wx.showModal({
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      });
    });
  },
  onShow: function() {},
  onHide: function() {},
  computed: {
    ...mapState(['userInfo', 'accessToken', 'position'])
  },
  methods: {
    ...mapMutations(['setStateAttr']),
    getLocation() {
      const _this = this;
      uni.getLocation({
        type: 'gcj02',
        geocode: true,
        success: function(res) {
          _this.currentPos = {
            latitude: res.latitude,
            longitude: res.longitude
          };
          _this.$qqmapsdk.reverseGeocoder({
            location: _this.currentPos,
            success: function(result) {
              _this.getAreaList(result.result);
            }
          });
        },
        fail: function(err) {
          _this.currentPos = {
            latitude: 39.870731,
            longitude: 116.375
          };
          _this.$qqmapsdk.reverseGeocoder({
            location: _this.currentPos,
            success: function(result) {
              _this.getAreaList(result.result);
            }
          });
        }
      });
    },
    async getAreaList(r) {
      const param = {};
      const res = await this.$api.getAreaList(param, '110100');
      if (res && res.code == 200) {
        let district = '';
        this.position.districtList = res.data;
        res.data.map((item, index) => {
          if (item.areaCode == r.ad_info.adcode) {
            district = item;
          }
        });
        let position = {
          province: { areaCode: '110000', areaName: '北京市' },
          provinceList: [],
          city: { areaCode: '110100', areaName: '北京' },
          cityList: [],
          district: district,
          districtList: res.data,
          lng: r.location.lng,
          lat: r.location.lat
        };
        this.setStateAttr({
          key: 'position',
          val: position
        });
      }
    }
  }
};
</script>

<style>
/*每个页面公共css */
@import './common/css/scss/base.scss';
/* vant */
/* @import "/wxcomponents/vant/dist/common/index.wxss"; */
</style>
